document.addEventListener('DOMContentLoaded', function () {
    const registerForm = document.getElementById('register-form');
    const modal = document.getElementById('myModal');
    const modalText = document.getElementById('modal-text');
    const span = document.getElementsByClassName('close')[0];
    const modalButton = document.getElementById('modal-button');
    registerForm.addEventListener('submit', function (event) {
        event.preventDefault();

        const formData = new FormData(registerForm);
        fetch('http://101.34.242.206:9990/users/register', {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(data => {
                let message = '';
                if (data.code === 200) {
                    message = '注册成功';
                } else {
                    message = '注册失败';
                }
                modalText.innerText = message;
                modal.style.display = 'block';
            })
            .catch((error) => {
                console.error('注册请求失败:', error);
                modalText.innerText = '注册请求失败';
                modal.style.display = 'block';
            });
    });
    modalButton.addEventListener('click', function() {
        window.location.href = 'login.html'; // 设置跳转的目标页面地址
    });
    span.onclick = function() {
        modal.style.display = 'none';
    }

    window.onclick = function(event) {
        if (event.target === modal) {
            modal.style.display = 'none';
        }
    }
});
